package com.sharad.quizbowl.ui.server;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.common.base.Joiner;
import com.sharad.quizbowl.ui.dao.dao.TossupsDao;
import com.sharad.quizbowl.ui.dao.dto.Tossups;
import com.sharad.quizbowl.ui.dao.factory.TossupsDaoFactory;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.json.JsonHierarchicalStreamDriver;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class RandomServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2298674233381890264L;

	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		TossupsDao dao = TossupsDaoFactory.create();
		PrintWriter writer = null;
		response.setContentType("text/javascript;charset=utf-8");
		String alt = request.getParameter("alt");
		try {
			writer = response.getWriter();
			try {
				List<String> queryTerms = new ArrayList<String>();
				List<String> queryValues = new ArrayList<String>();
				if (request.getParameter("condition") != null) {
					if (request.getParameter("condition").equals("all")) {
						queryTerms.add("answer like ? or question like ?");
						queryValues.add("%"
								+ request.getParameter("answer").replaceAll(
										" ", "%") + "%");
						queryValues.add("%"
								+ request.getParameter("answer").replaceAll(
										" ", "%") + "%");
					}
				} else {
					if (request.getParameter("answer") != null) {
						queryTerms.add("answer like ?");
						queryValues.add("%"
								+ request.getParameter("answer").replaceAll(
										" ", "%") + "%");
					} else {
						queryTerms.add("answer like ?");
						queryValues.add("%%");
					}
					if (request.getParameter("question") != null) {
						queryTerms.add("question like ?");
						queryValues.add("%"
								+ request.getParameter("question").replaceAll(
										" ", "%") + "%");
					}

				}
				if (request.getParameter("tournament") != null) {
					queryTerms.add("tournament like ?");
					queryValues.add(request.getParameter("tournament"));
				}

				if (request.getParameter("tournament") != null) {
					queryTerms.add("tournament like ?");
					queryValues.add(request.getParameter("tournament"));
				}
				if (request.getParameter("round") != null) {
					queryTerms.add("round like ?");
					queryValues.add(request.getParameter("round"));
				}
				if (request.getParameter("year") != null) {
					queryTerms.add("year like ?");
					queryValues.add(request.getParameter("year"));
				}
				if (request.getParameter("category") != null) {
					queryTerms.add("category like ?");
					queryValues.add(request.getParameter("category"));
				}
				if (request.getParameter("questionNum") != null) {
					queryTerms.add("question_num like ?");
					queryValues.add(request.getParameter("questionNum"));
				}
				if (request.getParameter("difficulty") != null) {
					queryTerms.add("difficulty like ?");
					queryValues.add(request.getParameter("difficulty"));

				}
				Tossups[] s = dao.findByDynamicWhere(
						Joiner.on(" and ").join(queryTerms)
								+ " order by rand() limit 1",
						queryValues.toArray(new String[] {}));
				XStream xstream = null;

				if (alt == null) {
					xstream = new XStream(new DomDriver());
					xstream.setMode(XStream.NO_REFERENCES);
					xstream.alias("tossup", Tossups.class);
					writer.print(xstream.toXML(s));
				} else if (alt.equals("json-in-script")) {
					xstream = new XStream(new JsonHierarchicalStreamDriver());
					xstream.setMode(XStream.NO_REFERENCES);
					xstream.alias("tossup", Tossups.class);
					writer.print((request.getParameter("callback") == null ? "callback"
							: request.getParameter("callback"))
							+ "(");
					writer.print(xstream.toXML(s));
					writer.print(")");
				}

			} catch (Throwable e) {
				writer.print("Error: query did not work!\n");
				writer.print(e.getMessage());

			}
			writer.flush();
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}
